import { useI18n } from '@/hooks/web/useI18n'
import { isFunction } from '@/utils/is'
import { firstUpperCase, humpToDash } from '@/utils'
import { set, get } from 'lodash'

const { t } = useI18n()

/**
 * 用于自动设置placeholder
 * @param schema 组件配置对象
 * @returns 占位符相关属性对象
 */
export function setTextPlaceholder(schema) {
  const textMap = [
    'Input',
    'Autocomplete',
    'InputNumber',
    'InputPassword'
  ]
  const selectMap = [
    'Select',
    'TimePicker',
    'DatePicker',
    'TimeSelect',
    'SelectV2'
  ]
  if (textMap.includes(schema?.component)) {
    return {
      placeholder: t('common.inputText')
    }
  }
  if (selectMap.includes(schema?.component)) {
    const twoTextMap = ['datetimerange', 'daterange', 'monthrange', 'datetimerange', 'daterange']
    const typeOrIsRange = (schema?.componentProps?.type || schema?.componentProps?.isRange) || ''
    if (twoTextMap.includes(typeOrIsRange)) {
      return {
        startPlaceholder: t('common.startTimeText'),
        endPlaceholder: t('common.endTimeText'),
        rangeSeparator: '-'
      }
    } else {
      return {
        placeholder: t('common.selectText')
      }
    }
  }
  return {}
}

/**
 * 合并栅格属性
 * @param col 栅格属性
 * @returns 合并后的栅格属性
 */
export function setGridProp(col = {}) {
  const colProps = {
    ...(col.span ? {} : {
      xs: 24,
      sm: 12,
      md: 12,
      lg: 12,
      xl: 12
    }),
    ...col
  }
  return colProps
}

/**
 * 设置组件的 props，默认添加 clearable 并处理事件
 * @param item 组件配置对象
 * @returns 处理后的组件属性对象
 */
export function setComponentProps(item) {
  const onEvents = item?.componentProps?.on || {}
  const newOnEvents = {}

  for (const key in onEvents) {
    if (onEvents[key]) {
      newOnEvents[`on${firstUpperCase(key)}`] = (...args) => {
        onEvents[key](...args)
      }
    }
  }

  const componentProps = {
    clearable: true,
    ...item.componentProps,
    ...newOnEvents
  }

  if (componentProps.slots) {
    delete componentProps.slots
  }
  if (componentProps.on) {
    delete componentProps.on
  }

  return componentProps
}

/**
 * 转换插槽属性，将驼峰转横杠
 * @param slotsProps 插槽对象
 * @returns 转换后的插槽对象
 */
export function setItemComponentSlots(slotsProps = {}) {
  const slotObj = {}
  for (const key in slotsProps) {
    if (slotsProps[key]) {
      if (isFunction(slotsProps[key])) {
        slotObj[humpToDash(key)] = (...args) => {
          return slotsProps[key](...args)
        }
      } else {
        slotObj[humpToDash(key)] = () => slotsProps[key]
      }
    }
  }
  return slotObj
}

/**
 * 根据 schema 初始化表单数据 model
 * @param schema 表单结构数组
 * @param formModel 原始表单数据对象
 * @returns 新的表单数据对象
 */
export function initModel(schema, formModel) {
  const model = { ...formModel }
  schema.forEach(v => {
    if (v.remove) {
      delete model[v.field]
    } else if (v.component !== 'Divider') {
      const hasField = get(model, v.field)
      set(model, v.field, hasField !== undefined ? get(model, v.field) : (v.value !== undefined ? v.value : undefined))
    }
  })

  // schema 中没有的字段，删除 model 里对应字段
  for (let i = 0; i < schema.length; i++) {
    const key = schema[i].field
    if (!Object.prototype.hasOwnProperty.call(model, key)) {
      delete model[key]
    }
  }
  return model
}
